package com.atguigu.domain;

import java.util.List;

/**
 * paging tool
 * @author dl
 * @date 2020/4/26 12:48
 */
public class Page<T> {
    //===============need user set
    /** 当前页 - current page */
    private int currentPage;
    /** 每页几条 - pageSize for page have several items */
    private int pageSize = 4;
    //==========
    /** 索引 - use for query database */
    private int queryIndex;
    /** 是否有上一页 - hasPrev */
    private boolean hasPrev;
    /** 是否有下一页 - hasNext */
    private boolean hasNext;
    /** 页面对象 - page items  */
    private List<T> items;
    //============
    /** 总页数 - totalPage */
    private int totalPage;
    //===========query
    /** 总记录数 - totalCount */
    private int totalCount;

    /** 记录 url */
    private String url;



    /** empty constructor */
    public Page() {
    }

//    public Page(int currentPage, int pageSize) {
//        this.currentPage = currentPage;
//        this.pageSize = pageSize;
//    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        /* 数据边界检查 */
        if ( currentPage < 1 ) {
            currentPage = 1;
        }
        if ( currentPage > getTotalPage()) {
            currentPage = getTotalPage();
        }
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getIndex() {
        /** 查询那一页   开始索引   结束索引
         *      1        0          3
         *      2        4          7
         *      3        8          11
         *      4        12         15
         *  (currentPage - 1) * pageSize
         */
        int index = (getCurrentPage() - 1) * getPageSize();
        if (index < 0) {
            index = 0;
        }
        return index;
    }
    // todo not use toString()
    public int getTotalPage() {
        // if the value not is integer
        this.totalPage = getTotalCount()/getPageSize();
        if (totalCount % pageSize > 0) {
            totalPage+=1;
        }
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public boolean isHasPrev() {
        return getCurrentPage() > 1;
    }

    public boolean isHasNext() {
        return getCurrentPage() < getTotalPage();
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}
